package org.suanfaTranning.ST200_Chatpter1_LineTable_HashTable_String.array;

import org.jikeshijian.Solution;

import java.util.HashSet;

/**
 * @Auther: qingle
 * @Date: 2024/9/11-17:11
 * @Description:
 * @version: 1.0
 */
public class Solution_lengthOfLongestSubstring1 {
	public static int lengthOfLongestSubstring(String s) {
		int n = s.length();
		int maxLen = 0; // 用于记录最长不重复子串的长度
		HashSet<Character> set = new HashSet<>(); // 用于存储子串中的字符
		int start = 0; // 子串的开始位置

		for (int end = 0; end < n; end++) {
			// 当前字符已经在子串中出现过，移动start指针
			while (set.contains(s.charAt(end))) {
				set.remove(s.charAt(start));
				start++;
			}
			// 将当前字符加入到子串中
			set.add(s.charAt(end));
			// 更新最长不重复子串的长度
			maxLen = Math.max(maxLen, end - start + 1);
		}

		return maxLen;
	}
		public static void main(String[] args) {
			Solution solution = new Solution();
			String input1 = "abcabcbb";
			System.out.println("Input: \"" + input1 + "\", Output: " + Solution_lengthOfLongestSubstring1.lengthOfLongestSubstring(input1));

			String input2 = "bbbbb";
			System.out.println("Input: \"" + input2 + "\", Output: " + Solution_lengthOfLongestSubstring1.lengthOfLongestSubstring(input2));

			String input3 = "pwwkew";
			System.out.println("Input: \"" + input3 + "\", Output: " + Solution_lengthOfLongestSubstring1.lengthOfLongestSubstring(input3));
		}
	}